package net.kesumu.general.android.opengl.object;

import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.BASE_RADIUS;
import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.ITEM_BG_FG_WIDTH_DELT;
import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.ITEM_BG_WIDTH;
import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.ITEM_FG_WIDTH;
import static net.kesumu.general.android.opengl.util.Constants.BYTES_PER_FLOAT;

import java.util.List;

import net.kesumu.general.android.opengl.object.Geometry.Point;
import net.kesumu.general.android.opengl.object.Geometry.RingSector;
import net.kesumu.general.android.opengl.object.ObjectBuilder.DrawCommand;
import net.kesumu.general.android.opengl.object.ObjectBuilder.GeneratedData;
import net.kesumu.general.android.opengl.program.TextureShaderProgram;
import net.kesumu.general.android.opengl.util.VertexArray;

public class YushabuMessageContent {
    
    private static final int POSITION_COMPONENT_COUNT = 3;
    private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
    private static final int STRIDE = (POSITION_COMPONENT_COUNT 
        + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT;
    
    public static final int INITIATED = 0,
                            APPEARING = 1,
                            APPEARED = 2,
                            LEAVING = 3,
                            LEFT = 4;
    
    public int state;
    public final int texture;
    public final int leftTexture;
    public final String message;
    
    public float itemNumber;
    public int color;
    public int numPoints;
    
    private final VertexArray vertexArray;
    private final List<DrawCommand> drawList;

    public YushabuMessageContent(String message, float itemNumber, int texture,int leftTexture, int numPoints) {
        this.state = INITIATED;
        this.itemNumber = itemNumber;
        this.texture = texture;
        this.leftTexture = leftTexture;
        this.message = message;
        
        //Keep the font width the same, but not the good resolution
        /*float startAngle = 280f - 0.3f * itemNumber;
        float endAngle = 360f - 3f * itemNumber;*/
        
        float startAngle = 280f;
        float endAngle = 360f;
        
        RingSector fgRingSector = new RingSector(new Point(0f, 0f, 0f), 
                BASE_RADIUS + itemNumber * ITEM_BG_WIDTH + ITEM_BG_FG_WIDTH_DELT, 
                BASE_RADIUS + itemNumber * ITEM_BG_WIDTH + ITEM_BG_FG_WIDTH_DELT + ITEM_FG_WIDTH, startAngle, endAngle);
    
        GeneratedData generatedData = ObjectBuilder.createYushabuMessageContent(fgRingSector, numPoints);

        vertexArray = new VertexArray(generatedData.vertexData);
        drawList = generatedData.drawList;
    }
    
    public void bindData(TextureShaderProgram textureProgram) {
        vertexArray.setVertexAttribPointer(
            0, 
            textureProgram.getPositionAttributeLocation(), 
            POSITION_COMPONENT_COUNT,
            STRIDE);
        
        vertexArray.setVertexAttribPointer(
            POSITION_COMPONENT_COUNT, 
            textureProgram.getTextureCoordinatesAttributeLocation(),
            TEXTURE_COORDINATES_COMPONENT_COUNT, 
            STRIDE);
    }
    
    public void draw() {
        for (DrawCommand drawCommand : drawList) {
            drawCommand.draw();
        }
    }

}
